<?php
	
	$_userid = $_userid == 1 && $userid  ? $userid : $_userid; // 管理员编辑其他会员信息权限
	
	if(!isset($action)) $action = 'manage';
	require_once 'form.class.php';
	require_once 'includes/yellowpage.class.php';
	$yellowpage = new yellowpage();
	$yellowpage->set_model('news');
	foreach($MODEL AS $modelid=>$value)
	{
		if($value['modeltype']==9 && $value['tablename'] == 'news') break;
	}
	$yellowpage->modelid = $modelid;
	
	switch($action){
		
		case 'add':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
		 
		if($dosubmit)
		{
			/*if(in_array($_groupid,$M['add_check']))
			{
				$info['status'] = 99;
			}
			else
			{
				$info['status'] = 1;
			}*/
			$info['status'] = 99;
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module, '');
			$contentid = $yellowpage->add($info);
			
			// by aming 
			//$url = "?module=$module&action=web&userid=$_userid&category=news&id=$contentid";
			//$db->query("UPDATE `".DB_PRE."yellowpage_news` SET `url`='$url' WHERE `id`=$contentid");
			showmessage('发布成功！', $forward);
	
		}
		else
		{
			if($M['ischeck'] && $company_user_infos['status'] == 0) showmessage('您的公司正在审核当中...','goback');
			if($company_user_infos['endtime'] && $company_user_infos['endtime']<TIME) showmessage('您的服务截至日期已到，请续费...','goback');
			foreach($MODEL AS $modelid=>$value)
			{
				if($value['modeltype']==9 && $value['tablename'] == 'news') break;
			}
	
			require CACHE_MODEL_PATH.'yp_form.class.php';
			$content_form = new content_form($modelid);
			$data['catid'] = $catid;
			$forminfos = $content_form->get($data);
		}
	 	include template('news_add.html','yellowpage');
	break;
	
	case 'manage':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
		
		$infos = $yellowpage->listinfo(/*"userid='$userid'"*/1,'id DESC',$page);
		$pages = $yellowpage->pages;
		include template('news_manage.html','yellowpage');
	break;

	case 'edit':
	if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
	
		$id = intval($id);
		if($dosubmit)
		{
			require_once MOD_ROOT.'attachment/attachment.class.php';
			$attachment = new attachment($module);
			$yellowpage->edit($id, $info);
			
			// by aming 
			//$url = "?module=$module&action=web&userid=$_userid&category=news&id=$id";
			//$db->query("UPDATE `".DB_PRE."yellowpage_news` SET `url`='$url' WHERE `id`=$id");
			showmessage('修改成功！', $forward);
		}
		else
		{
			require CACHE_MODEL_PATH.'yp_form.class.php';
			$content_form = new content_form($modelid);
			$data = $yellowpage->get($id);
			$forminfos = $content_form->get($data);
		}
		include template('news_edit.html','yellowpage');
		break;
	case 'delete':
		if(!$_userid) showmessage('您还没有登陆，即将跳转到登陆页面',url("?module=member&action=login&forward=".urlencode(URL)));
		
		$id = intval($id);
		$yellowpage->delete($id);
		showmessage('删除成功！', $forward);
		include template('news_manage.html','yellowpage');
		break;
		
	
	case 'show':
		
		cache_page_start();
			
		$readproid = get_cookie('readproid');
		if(intval($readproid)) $prowhere = $readproid;
		require_once 'includes/yellowpage.class.php';
		require_once 'includes/company.class.php';
		$company = new company();
		$yp = new yellowpage();
		$newsid = intval($id);
		if(!$newsid) exit('非法参数');
		$yp->set_model('news');
		$rs = $yp->get($newsid);
		if($rs['status'] != 99) showmessage('信息正在审核中...');
		$head['keywords'] .= $rs['keywords'].'_新闻';
		$head['description'] .= $rs['title'].'_新闻'.'_'.$SETTING['sitename'];
		$head['title'] .= $rs['title'].'_新闻'.'_'.$SETTING['sitename'];
		$c = $company->get($rs['userid']);
		$key_words_array = explode(" ",$rs['keywords']);
		$key_words_array = array_unique($key_words_array);
		if(count($key_words_array))
		{
			$news_where = '';
			foreach($key_words_array as $nid => $np)
			{
				$np = addslashes(htmlspecialchars($np));
				if($np)
				{
					$news_where .= $nid ? " OR keywords LIKE '%{$np}%'" : " keywords LIKE '%{$np}%'";
				}
			}		
		}
		if(!$news_where)
		{
			$news_where .= "status = '99'";
		}
		else
		{
			$news_where .= " AND status = '99'";
		}
		include template('news_show.html','yellowpage');
		cache_page(intval($M['cache_list']));
	break;
		
		
		default:
			$catid = intval($catid);
			$head['keywords'] .= '新闻';
			$head['description'] .= '新闻'.'_'.$SETTING['sitename'];
			$head['title'] .= '新闻'.'_'.$SETTING['sitename'];
			$CAT = subcat(0,'yellowpage');
			if($catid)
			{
				if($child == 1) $arrchildid = subcat($catid,'yellowpage');
			}
			$view_type = max(intval($view_type), 1);
			$page = $page ? $page : 1;
			$pagesize = $pagesize ? $pagesize : 20;
			$where = " WHERE p.userid=c.userid AND p.status=99";
			if($catid)
			{
				if($CATEGORY[$catid]['arrchildid'])
					$where .= " AND p.catid IN (".$CATEGORY[$catid]['arrchildid'].")";
				else
					$where .= " AND p.catid='$catid'";
			}
			if($areaname)
			{
				$areaname = htmlspecialchars($areaname);
				if(strpos(',',$areaname)===false)
				{
					$where .= " AND c.areaname='$areaname'";
				}
				else
				{
					$where .= " AND c.areaname IN ($areaname)";
				}
				$areaname = urlencode($areaname);
			}
			else
			{
				$areaname = 0;
			}
			if(isset($order))
			{
				$order = intval($order);
				$orderby = $order ? 'ASC' : 'DESC';
				$sql = "SELECT * FROM `".DB_PRE."yellowpage_product` p,`".DB_PRE."member_company` c $where ORDER BY p.price {$orderby}";
			}
			else
			{
				$sql = "SELECT * FROM `".DB_PRE."yellowpage_product` p,`".DB_PRE."member_company` c $where ORDER BY p.id DESC";
			} 
			if($M['enable_rewrite'])
			{
				$urlrule = "$M[url]product-list-$view_type-$catid-$pagesize--$areaname--$order.html|$M[url]product-list-$view_type-$catid-$pagesize--$areaname--$order-\$page.html";
			}
			else
			{
				$urlrule = "$M[url]product.php?view_type=$view_type&catid=$catid&pagesize=$pagesize&areaname=$areaname&order=$order|$M[url]product.php?view_type=$view_type&catid=$catid&pagesize=$pagesize&areaname=$areaname&order=$order&page=\$page";
			}
			include template('news.html','yellowpage');
			cache_page(intval($M['cache_list']));
		break;
		
	
	}
?>